package Common 
{
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class Messages extends Sprite 
	{
		private var FMessage:AbstractMessage;
		private var FWidth:int;
		private var FHeight:int;
		
		public function Messages() 
		{			
			visible = false;
		}		
			
		public function Show(AMessage:AbstractMessage):void 
		{
			if (FMessage)
			{
				removeChild(FMessage);
				FMessage = null;
			}
			FMessage = AMessage;
			FMessage.x = (FWidth - FMessage.width) / 2;
			FMessage.y = (FHeight - FMessage.height) / 2;
			visible = true;
			alpha = 0;
			TweenLite.to(this, 0.5, {alpha: 1, onComplete:ShowMessage});
			FMessage.y -= 100;
			FMessage.alpha = 0;
			addChild(FMessage);
		}
		
		private function ShowMessage():void 
		{
			TweenLite.to(FMessage, 1, {alpha:1, y:(FHeight - FMessage.height) / 2});
		}		
		
		public function SetSize(AWidth:int, AHeight:int):void
		{
			FWidth = AWidth;
			FHeight = AHeight;
			graphics.clear();
			graphics.beginFill(Constants.MESSAGES_BACKGROUND, Constants.MESSAGES_BACKGROUND_ALPHA);
			graphics.drawRect(0, 0, AWidth, AHeight);
			graphics.endFill();
			if (FMessage)
			{
				TweenLite.killTweensOf(FMessage);
				FMessage.alpha = 1;
				FMessage.x = (AWidth - FMessage.width) / 2;
				FMessage.y = (AHeight - FMessage.height) / 2;
				FMessage.Resize();
			}
		}
	}
}